package com.turing.mission.controller;

import com.alibaba.fastjson.JSONObject;
import com.turing.mission.service.MissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import result.Result;

import java.text.ParseException;
@EnableScheduling
@RestController
public class MissionController {

    @Autowired
    private MissionService missionService;

    @GetMapping("/mi/miList")
    public Result miList(
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "merchantName", required = false) String merchantName,
            @RequestParam(value = "longitude", required = false) String longitude,
            @RequestParam(value = "latitude", required = false) String latitude,
            @RequestParam(value = "cityId", required = false) String cityId,
            @RequestParam(value = "merchantId", required = false) String merchantId,
            @RequestParam(value = "mtId", required = false) String mtId,
            @RequestParam(value = "sortType", required = false,defaultValue = "-1") Integer sortType,
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @RequestParam(value = "orderType") Integer orderType,
            @RequestParam(value = "pageSize", required = false) Integer pageSize
    ){
        return missionService.miList(startTime,endTime, merchantName, longitude, latitude,cityId,merchantId,mtId,sortType,orderType,pageNum,pageSize);
    }

    @GetMapping("/mi/geMissionById")
    public Result geMissionById(@RequestParam(value = "id", required = false) String id) throws ParseException {
        return missionService.geMissionById(id);
    }

    @PostMapping("/mi/missonSave")
    public Result missonSave(@RequestBody JSONObject jsonObject){
        return missionService.missonSave(jsonObject);
    }

    @PatchMapping("/mi/missonEdit")
    public Result missonEdit(@RequestBody JSONObject jsonObject){
        return missionService.missonEdit(jsonObject);
    }

    @PatchMapping("/mi/missonFreeze")
    public Result missonFreeze(@RequestBody JSONObject jsonObject){
        return missionService.missonFreeze(jsonObject);
    }

    @PatchMapping("/mi/missonSign")
    public Result missonSign(@RequestBody JSONObject jsonObject) throws ParseException {
        return missionService.missonSign(jsonObject);
    }

    @GetMapping("/mi/missionGoodState")
    public Result missionGoodState(@RequestParam(value = "missionId") String missionId){
        return missionService.missionGoodState(missionId);
    }

    @Scheduled(cron = "0 45 9 * * ?")
    public void simpleSchedule(){
        missionService.simpleSchedule();
    }

    @GetMapping("/mi/checkTitle")
    public boolean checkTitle(
            @RequestParam(value = "merchantId") String merchantId,
            @RequestParam(value = "missionTitle") String missionTitle,
            @RequestParam(value = "missionId",required = false) String missionId){
        return missionService.checkTitle(merchantId,missionTitle,missionId);
    }

    @GetMapping("/mi/miListByIds")
    public Result miListByIds(@RequestParam(value = "ids") String[] ids){
        return missionService.miListByIds(ids);
    }

}
